using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Text;
using System.Web;
using System.Web.Caching;
using System.Web.Profile;
using TestSystem.BLL;
using TestSystem.BLL.Profile;
using TestSystem.BLL.TestSystem;


namespace TestSystem.BLL.Helpers
{
    public class UIHelper : BusinessEntity
    {
        private static string GetUserDetails(int albumID, string userName)
        {
            string key = "UsersDetailsForComments_" + albumID;
            if (Cache[key] != null)
            {
                var usersDetails = (Hashtable)Cache[key];
                if (usersDetails.Contains(userName))
                {
                    var pf = (CustomProfile)usersDetails[userName];
                    return pf.Personal.FirstName + "||" + pf.Personal.LastName + "||" + "../" + pf.Forum.AvatarUrl;
                }
                else
                {
                    var pf = (CustomProfile)ProfileBase.Create(userName);
                    usersDetails.Add(userName, pf);
                    CacheData(key, usersDetails, 3000, -1);
                    return pf.Personal.FirstName + "||" + pf.Personal.LastName + "||" + "../" + pf.Forum.AvatarUrl;
                }
            }
            else
            {
                var usersDetails = new Hashtable();
                var pf = (CustomProfile)ProfileBase.Create(userName);
                usersDetails.Add(userName, pf);
                CacheData(key, usersDetails, 3000, -1);
                return pf.Personal.FirstName + "||" + pf.Personal.LastName + "||" + "../" + pf.Forum.AvatarUrl;
            }
        }

        public static string RenderQuestionPanel(Test currentTest, List<UserAnswer> userAnswers)
        {
            var output = new StringBuilder();
            var answers = currentTest.Answers;
            if (answers != null)
            {
                output.Append("<pre class=\"q-text\">");
                output.Append(currentTest.Body);
                output.Append("</pre>");
                switch (currentTest.TestType)
                {
                    case 1:
                        output.Append("<ul class=\"answers\">");
                        for (int i = 0; i < answers.Count; i++)
                        {
                            output.Append("<li><input type='radio' name='answer' id='" + answers[i].AnswerID +
                                          "' value='" + answers[i].AnswerID + "' ");
                            if (userAnswers != null)
                            {
                                if (userAnswers.Find(answer => answer.UserAnswerID == answers[i].AnswerID) != null)
                                    output.Append("checked='true'");
                            }
                            output.Append("/>");
                            output.Append("<label for='" + answers[i].AnswerID + "'>" + answers[i].Body + "</label>");
                            output.Append("</li>");
                        }
                        output.Append("</ul>");
                        break;
                    case 2:
                        output.Append("<ul>");
                        for (int i = 0; i < answers.Count; i++)
                        {
                            output.Append("<li><input type='checkbox' name='answer' id='" + answers[i].AnswerID +
                                          "' value='" + answers[i].AnswerID + "' ");
                            if (userAnswers != null)
                            {
                                if (userAnswers.Find(answer => answer.UserAnswerID == answers[i].AnswerID) != null)
                                    output.Append("checked='true'");
                            }
                            output.Append("/>");
                            output.Append("<label for='" + answers[i].AnswerID + "'>" + answers[i].Body + "</label>");
                            output.Append("</li>");
                        }
                        output.Append("</ul>");
                        break;
                    case 3:
                        output.Append("<br/>");
                        output.Append("<input type='text' ");
                        if (userAnswers != null)
                        {
                            output.Append("value='" + userAnswers[0].UserAnswerBody + "'");
                        }
                        output.Append("/>");
                        break;
                    case 4:
                        goto case 3;
                    default:
                        break;
                }
            }
            return output.ToString();
        }

        protected override void Load(IDataReader rdr)
        {
            throw new NotImplementedException();
        }

        public override void Update()
        {
            throw new NotImplementedException();
        }
    }

}

